package cn.e4j.modular.system.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.system.entity.SysDic;
import cn.e4j.modular.system.service.SysDicService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 用户管理
 *
 * @author wenchuan.li
 * @Date 2018/12/08 16:03
 */
@Controller
@RequestMapping("/sys/dic")
public class SysDicController extends BaseController {


    @Autowired
    private SysDicService sysDicService;

    /**
     * 跳转用户管理页面
     */
    @RequiresPermissions("sys:dic:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/dic.html";
    }

    /**
     * 跳转到查看新增页面
     */
    @RequestMapping("/dic_add")
    public String addView() {
        return "/pc/system/dic_edit.html";
    }

    /**
     * 查询字典信息列表
     */
    @RequestMapping("/getDiclist")
    @RequiresPermissions("sys:dic:init")
    @ResponseBody
    public ResponseData getDiclist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysDicService.getDicList(page, inDto);
        return new PageReData(page, result);
    }


    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:dic:init")
    //@BussinessLog(value = "修改用户", key = "account", dict = UserDict.class)
    @ResponseBody
    public ResponseData edit(SysDic dic, BindingResult result) {

        // 注意：user 前端只能传需要修改值和主键值，以防把不需要修改的值改成空值
        dic.updateById();

        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:dic:init")
    //@BussinessLog(value = "修改用户", key = "account", dict = UserDict.class)
    @ResponseBody
    public ResponseData del(SysDic dic, BindingResult result) {

        String id = dic.getID();
        // 注意：user 前端只能传需要修改值和主键值，以防把不需要修改的值改成空值
        dic.deleteById();

        return SUCCESS_TIP;
    }


    /**
     * 添加字典信息
     */
    @RequestMapping("/add")
    //@BussinessLog(value = "添加用户", key = "account", dict = UserDict.class)
    @RequiresPermissions("sys:dic:init")
    @ResponseBody
    public ResponseData add(SysDic dic, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        // 判断字典信息是否重复

        SysDic theDic = sysDicService.getById(dic.getID());
        if (theDic != null) {
            throw new ServiceException(BizExceptionEnum.DICT_EXISTED);
        }

        dic.insert();
        return SUCCESS_TIP;
    }

}
